import random
import pgzrun
import tkinter as tk
import tkinter.font as tkFont
import tkinter.messagebox as tkMsgBox
import _thread

WIDTH = 720
HEIGHT = 500

BACK_COLOR = (255 ,255, 255)
done=False
nclr=(0,0,0)

def tktk():
    global nclr
    top=tk.Tk()
    top.title("颜色面板")
    top.resizable(False,False)
    defaultFont = tkFont.Font(top, size = 10)
    fpx = 10
    fpy = 10
    rStr = tk.StringVar()
    gStr = tk.StringVar()
    bStr = tk.StringVar()
    def OnOK():
        global nclr,clrl,M
        if rStr.get()=='' or gStr.get()=='' or bStr.get()=='':
            tkMsgBox.showerror('error', '颜色错误')
            return
        r=int(rStr.get())
        g=int(gStr.get())
        b=int(bStr.get())
        if r>255 or g>255 or b>255:
            tkMsgBox.showerror('error', '颜色错误')
            return
        nclr=(r,g,b)
        clrl.insert(0,nclr)
        M.y=HEIGHT//20
        
    userFrame= tk.Frame(top)
    userFrame.pack(side = tk.TOP, fill=tk.X, padx = fpx, pady=fpy)
    userLabel = tk.Label(userFrame, text='R:', width=3, anchor=tk.E, font=defaultFont)
    userLabel.pack(side=tk.LEFT)


    userEntry = tk.Entry(userFrame, width=3, font=defaultFont, textvariable=rStr)
    userEntry.pack(side=tk.LEFT)

    pwdFrame= tk.Frame(top)
    pwdFrame.pack(side=tk.TOP, fill=tk.X, padx = fpx, pady=fpy)
    pwdLabel = tk.Label(pwdFrame, text='G:', width=3, anchor=tk.E, font=defaultFont)
    pwdLabel.pack(side=tk.LEFT)


    pwdEntry = tk.Entry(pwdFrame, width=3, font=defaultFont, textvariable=gStr)
    pwdEntry.pack(side=tk.LEFT)


    pwdFrame= tk.Frame(top)
    pwdFrame.pack(side=tk.TOP, fill=tk.X, padx = fpx, pady=fpy)
    pwdLabel = tk.Label(pwdFrame, text='B:', width=3, anchor=tk.E, font=defaultFont)
    pwdLabel.pack(side=tk.LEFT)


    pwdEntry = tk.Entry(pwdFrame, width=3, font=defaultFont, textvariable=bStr)
    pwdEntry.pack(side=tk.LEFT)

    btnFrame = tk.Frame(top)
    btnFrame.pack(side=tk.TOP, fill=tk.X, padx=fpx, pady=fpy)

    confirmBtn = tk.Button(btnFrame, width=8, text='确认', font=defaultFont, command=OnOK)
    confirmBtn.pack(side=tk.TOP)

    top.mainloop()

class MyRect:
    def __init__(self, x, y, w, h, clr):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.clr = clr
        self.speed = 2
    def SetSpeed(self, speed):
        self.speed = speed

    def Update(self):
        pass

    def GetGridPos(self):
        gx = self.x // self.w
        gy = self.y // self.h

        return gx, gy

    def Draw(self):
        rc = Rect((self.x - self.w // 2, self.y - self.h // 2), (self.w, self.h))
        screen.draw.filled_rect(rc, self.clr)

rc_list = []
unit = 20
nclr=(0,0,0)
erace=False
clrl=[(0,0,0),
      (150,150,150),
      (220,220,220),
      (255,0,0),
      (255,100,0),
      (255,255,0),
      (0,255,0),
      (0,150,50),
      (0,0,255),
      (150,0,255)]
M=MyRect(WIDTH-10,HEIGHT//20,5,5,(0,0,0))

def on_key_down(key):
    global erace,M,nclr,clrl,rc_list,BACK_COLOR
    if key == keys.ESCAPE:
        quit()
    elif key == keys.SPACE:
        erace=not erace
    elif key == keys.DOWN:
        if M.y<HEIGHT-HEIGHT//20:
            M.y+=HEIGHT//10
    elif key == keys.UP:
        if M.y>HEIGHT//20:
            M.y-=HEIGHT//10
    elif key==keys.TAB:
        x=(M.y-HEIGHT//20)//(HEIGHT//10)
        if x==0:
            return
        nclr=clrl[x]
        M.y=HEIGHT//20
        clrl[0],clrl[x]=clrl[x],clrl[0]
    elif key==keys.BACKSPACE:
        x=(M.y-HEIGHT//20)//(HEIGHT//10)
        if x>=len(clrl):
            return
        if len(clrl)==1:
            return
        del clrl[x]
        if x==0:
            nclr=clrl[0]
    elif key==keys.DELETE:
        rc_list=[]
    elif key==keys.B:
        x=(M.y-HEIGHT//20)//(HEIGHT//10)
        BACK_COLOR=clrl[x]
    
def on_mouse_down(pos):
    global done
    done=True
    x = pos[0]
    y = pos[1]
    gx = x // unit
    gy = y // unit
    clr=nclr
    if erace==True:
        clr=BACK_COLOR
    Rc=MyRect(gx*unit+unit//2,gy*unit+unit//2,unit,unit,clr)
    rc_list.append(Rc)

def on_mouse_up(pos):
    global done
    done=False

def on_mouse_move(pos):
    if not done:
        return
    x = pos[0]
    y = pos[1]
    gx = x // unit
    gy = y // unit
    clr=nclr
    if erace==True:
        clr=BACK_COLOR
    Rc=MyRect(gx*unit+unit//2,gy*unit+unit//2,unit,unit,clr)
    rc_list.append(Rc)
    
def update():
    pass

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rc_list:
        rc.Draw()
    for i in range(0,WIDTH-20,unit):
        screen.draw.line((i,0),(i,HEIGHT),(200,200,200))
    for i in range(0,HEIGHT,unit):
        screen.draw.line((0,i),(WIDTH-20,i),(200,200,200))
    n=len(clrl)
    if n>10:
        n=10
    for i in range(0,n):
        hrc=MyRect(WIDTH-10,HEIGHT//20+(i*(HEIGHT//10)),20,HEIGHT//10,clrl[i])
        hrc.Draw()
    BM=MyRect(M.x,M.y,7,7,(255,255,255))
    BM.Draw()
    M.Draw()

_thread.start_new_thread(tktk, ())
pgzrun.go()
